package org.wmq.taskschedule.algorithm;

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

/**
 * 图像缩放算法执行器
 * 实现AlgorithmExecutor接口，用于对图像进行缩放处理
 */
public class ImageResizeExecutor implements AlgorithmExecutor {

    private int newWidth; // 新宽度
    private int newHeight; // 新高度
    private int interpolationMethod; // 插值方法
    private String description; // 算法描述

    /**
     * 默认构造函数，使用默认参数
     */
    public ImageResizeExecutor() {
        this.newWidth = 640;
        this.newHeight = 480;
        this.interpolationMethod = Imgproc.INTER_LINEAR;
        this.description = "图像缩放算法 - 调整图像尺寸大小";
    }

    /**
     * 构造函数，允许自定义参数
     * 
     * @param newWidth 新宽度
     * @param newHeight 新高度
     */
    public ImageResizeExecutor(int newWidth, int newHeight) {
        if (newWidth <= 0 || newHeight <= 0) {
            throw new IllegalArgumentException("宽度和高度必须大于0");
        }
        this.newWidth = newWidth;
        this.newHeight = newHeight;
        this.interpolationMethod = Imgproc.INTER_LINEAR;
        this.description = "图像缩放算法 - 调整图像尺寸大小";
    }

    /**
     * 构造函数，允许自定义所有参数
     * 
     * @param newWidth 新宽度
     * @param newHeight 新高度
     * @param interpolationMethod 插值方法
     * @param description 算法描述
     */
    public ImageResizeExecutor(int newWidth, int newHeight, int interpolationMethod, String description) {
        if (newWidth <= 0 || newHeight <= 0) {
            throw new IllegalArgumentException("宽度和高度必须大于0");
        }
        this.newWidth = newWidth;
        this.newHeight = newHeight;
        this.interpolationMethod = interpolationMethod;
        this.description = description;
    }

    @Override
    public Mat execute(Mat input) {
        if (input == null || input.empty()) {
            throw new IllegalArgumentException("输入图像不能为空");
        }

        // 创建输出图像
        Mat output = new Mat();

        // 应用图像缩放算法
        Imgproc.resize(input, output, new Size(newWidth, newHeight), 0, 0, interpolationMethod);

        return output;
    }

    // Getters and setters
    public int getNewWidth() {
        return newWidth;
    }

    public void setNewWidth(int newWidth) {
        if (newWidth <= 0) {
            throw new IllegalArgumentException("宽度必须大于0");
        }
        this.newWidth = newWidth;
    }

    public int getNewHeight() {
        return newHeight;
    }

    public void setNewHeight(int newHeight) {
        if (newHeight <= 0) {
            throw new IllegalArgumentException("高度必须大于0");
        }
        this.newHeight = newHeight;
    }

    public int getInterpolationMethod() {
        return interpolationMethod;
    }

    public void setInterpolationMethod(int interpolationMethod) {
        this.interpolationMethod = interpolationMethod;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}